<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算属性的完整写法</title>
  <script src="../vue.js"></script>
  <style>
    /* input {
      width: 30px;
    } */
  </style>
</head>

<body>
  <!-- 
    计算属性
    默认的写法只能读取访问,不能修改
    如果要修改数据,就要用完整的写法
   -->
  <div id="app">
    姓：<input type="text" v-model="firstName"> +
    名：<input type="text" v-model="lastName"> =
    <span>{{ fullName }}</span><br><br>

    <input width="100px" type="text" v-model="username" placeholder="请输入要改的名字">
    <button @click="changeName">改名卡</button>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        firstName: '',
        lastName: '',
        username: ''
      },
      methods: {
        changeName() {
          this.fullName = this.username
        }
      },
      computed: {
        // 完整写法写成一个对象,里面写get和set方法
        fullName: {
          get() {
            return this.firstName + this.lastName
          },
          set(value) {
            // 这个value就是改变的值
            console.log(value);
            this.firstName = value.slice(0, 1)
            this.lastName = value.slice(1)
          }
        }
      }
    })
  </script>
</body>

</html>